package com.multiplayer.service.impl;


import java.util.ArrayList;
import java.util.List;


import com.multiplayer.dao.IAdminDao;
import com.multiplayer.dao.IStudyRoomDao;
import com.multiplayer.dao.impl.AdminDaoImpl;
import com.multiplayer.dao.impl.StudyRoomDaoImpl;
import com.multiplayer.entity.StudyRoom;
import com.multiplayer.service.IStudyRoomService;

import com.multiplayer.tools.PageData;



public class StudyRoomServiceImpl implements IStudyRoomService{
	IStudyRoomDao studyRoomDao=new StudyRoomDaoImpl();
	/**
	 * 添加自习室
	 */
	@Override
	public boolean addStudyRoom(StudyRoom studyRoom) {
		// TODO Auto-generated method stub
		//如果要添加管理员但不存在该管理员则添加失败
		IAdminDao adminDao=new AdminDaoImpl();
		if(studyRoom.getAno()!=null&&adminDao.queryAdminById(studyRoom.getAno()).size()==0)
		{
			return false;
		}
		//可预约座位数不能大于总座位数
		if(studyRoom.getRfreeseat()>studyRoom.getRseatnum())
		{
			return false;
		}
		//如果开放时间大于关闭时间则添加失败
		long timeGaps=0;
		if(studyRoom.getRetime()!=null&&studyRoom.getRstime()!=null)
		{
			timeGaps=studyRoom.getRetime().getTime()-studyRoom.getRstime().getTime();
		}
		if(timeGaps<0)
		{
			return false;
		}
		//如果已存在该自习室则添加失败

		if(studyRoomDao.queryStudyRoomById(studyRoom.getRno()).size()>0)
		{
			return false;
		}
		if(studyRoomDao.addStudyRoom(studyRoom))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * 删除自习室
	 */
	@Override
	public boolean deleteStudyRoomById(String studyRoomId) {
		// TODO Auto-generated method stub
		if(studyRoomDao.deleteStudyRoomById(studyRoomId))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	 /**
     * 修改自习室信息
     */
	@Override
	public boolean editStudyRoom(StudyRoom studyRoom) {
		// TODO Auto-generated method stub
		//如果不存在该自习室，则修改自习室失败
		if(studyRoomDao.queryStudyRoomById(studyRoom.getRno()).size()==0)
		{
			return false;
		}
		//如果修改了自习室管理员，但不存在该管理员则修改失败
		IAdminDao adminDao=new AdminDaoImpl();
		if(studyRoom.getAno()!=null&&adminDao.queryAdminById(studyRoom.getAno()).size()==0)
		{
			return false;
		}
		//如果开放时间大于关闭时间则修改失败
		long timeGaps=0;
		if(studyRoom.getRetime()!=null&&studyRoom.getRstime()!=null)
		{
			timeGaps=studyRoom.getRetime().getTime()-studyRoom.getRstime().getTime();
		}
		if(timeGaps<0)
		{
			return false;
		}

		if(studyRoomDao.editStudyRoom(studyRoom))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 通过模糊查询获得自习室信息
	 */
	@Override
	public List<StudyRoom> queryStudyRoomByKeywords(String keywords) {
		// TODO Auto-generated method stub
		List<StudyRoom> list=new ArrayList<StudyRoom>();
		list=studyRoomDao.queryStudyRoomByKeywords(keywords);
		return list;
	}

	/**
	 * 通过自习室编号查询自习室信息
	 */
	@Override
	public List<StudyRoom> queryStudyRoomById(String studyRoomId) {
		// TODO Auto-generated method stub
		List<StudyRoom> list=new ArrayList<StudyRoom>();
		list=studyRoomDao.queryStudyRoomById(studyRoomId);
		return list;
	}
	/**
	 * 关闭自习室
	 */
	@Override
	public boolean closeStudyRoom(StudyRoom studyRoom) {
		// TODO Auto-generated method stub
		studyRoom.setRstatus("关闭");
		studyRoom.setRstime(null);
		studyRoom.setRetime(null);
		studyRoom.setRfreeseat(0);
		if(studyRoomDao.editStudyRoom(studyRoom))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}

	/**
	 * 通过模糊查询分页显示自习室信息
	 */
	@Override
	public PageData<StudyRoom> getStudyRoomByPage(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		//当前页码不能小于1
		if(page<1)
		{page=1;}
		PageData<StudyRoom> pd = studyRoomDao.getStudyRoomByPage(page, pageSize, keywords);
		return pd;
	}

}
